﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace websitev2.Core
{
    public class Image
    {
        /*
         * Image Type
         * 1 = Avatar 70x70
         * 2 = Avatar 35x35
         * 100 = App Large
         * 101 = App  Medium
         * 102 = App Small
         */


        /// <summary>
        /// 
        /// </summary>
        /// <param name="LinkedGUID"></param>
        /// <param name="LinkedID"></param>
        /// <param name="Location"></param>
        /// <param name="FileName"></param>
        /// <param name="ImageType"></param>
        /// <param name="Update">if this is set to true then we'll first check that the record doesn't exist and if it does we'll update it, rather than create a new one</param>
        /// <returns></returns>
        public static bool WriteImageRecord(Guid LinkedGUID, Int64 LinkedID, string Location, string FileName, int ImageType, bool Update)
        {


            Models.StartStopEntities db = new Models.StartStopEntities();
            Models.SiteImage oImage = new Models.SiteImage();

            if (Update)
            {
                if (db.SiteImages.Any(a => a.LinkedGUID == LinkedGUID
                    && a.LinkedID == LinkedID
                    && a.ImageType == ImageType))
                {
                    // found
                    oImage = db.SiteImages.FirstOrDefault(a => a.LinkedGUID == LinkedGUID
                    && a.LinkedID == LinkedID
                    && a.ImageType == ImageType);
                }
                else
                {
                    Update = false; 
                }

            }


            oImage.CreatedOn = DateTime.Now;
            oImage.LinkedGUID = LinkedGUID;
            oImage.ImageType = ImageType;
            oImage.ImageGUID = Guid.NewGuid();
            oImage.FileName = FileName;
            oImage.LinkedID = LinkedID;
            oImage.Location = Location;

            if (!Update)
            {
                db.SiteImages.AddObject(oImage);
            }

            db.SaveChanges();

            return true;
        }

        // This is just the same as above, so should be refactored, but contains no GUID, where there's no associated GUID. 
        public static bool WriteImageRecord(Int64 LinkedID, string Location, string FileName, int ImageType, bool Update)
        {


            Models.StartStopEntities db = new Models.StartStopEntities();
            Models.SiteImage oImage = new Models.SiteImage();

            if (Update)
            {
                if (db.SiteImages.Any(a => a.LinkedID == LinkedID

                    && a.ImageType == ImageType))
                {
                    // found
                    oImage = db.SiteImages.FirstOrDefault(a => a.LinkedID == LinkedID

                    && a.ImageType == ImageType);
                }
                else // not found
                {
                    Update = false; 
                }

            }


            oImage.CreatedOn = DateTime.Now;

            oImage.ImageType = ImageType;
            oImage.ImageGUID = Guid.NewGuid();
            oImage.FileName = FileName;
            oImage.LinkedID = LinkedID;
            oImage.Location = Location;

            if (!Update)
            {
                db.SiteImages.AddObject(oImage);
            }

            db.SaveChanges();

            return true;
        }


        public static string CheckImageExists(Guid LinkedGUID, int ImageTypeID)
        {
            string _filename = SqlAccessCore.ExecuteScalar("select filename from SiteImages where LinkedGUID = '" + LinkedGUID.ToString() + "' and ImageType=" + ImageTypeID.ToString());
            return _filename;
        }

        public static string CheckImageExists(Int64 LinkedID, int ImageTypeID)
        {
            string _filename = SqlAccessCore.ExecuteScalar("select filename from SiteImages where LinkedID = '" + LinkedID.ToString() + "' and ImageType=" + ImageTypeID.ToString());
            return _filename;
        }


    }
}